var express = require("express");
var router = express.Router();
var fs = require('fs');
var path = require('path');
var router = express.Router();
// var bodyParser = require("body-parser");
// var urlEncoded = bodyParser.urlencoded({
// 	extended: false
// })
var query = require('./mysql')
// var connection = require('./mysql');

router.get("/pa", function(req, res, nex) {
	res.send({
		code: 200
	})
});
//userinfor请求
router.post('/', async (req, res) => {
	let data = req.body;
	let rows = await query( 'select * from userinfor where userphone=13203853392');
	// let json = {};
	if (rows.length !== 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
		// console.log(result)
	}
	
	res.send(json)
});
// 积分订单
router.post('/readorder', async (req, res) => {
    let data = req.body;
    let rows = await query("select * from pointsorder where state=\'1\'  ");
    let json = {};
    if (data) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});
// 积分订单分页跳转
router.post('/orderpage', async (req, res) => {
    let data = req.body;
    // var start = (data.page - 1) * 3;
    // let page = (data.page - 1) *3;
    let rows = await query("select * from pointsorder limit 3");
    let json = {};
    if (data) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});
// 删除积分订单。
router.post('/delorder', async (req, res) => {
    let data = req.body;
    console.log(data.id);
    let rows = await query('update pointsorder set state=\'0\' where id=' + data.id + ";");
    let json = {};
    if (!rows.length == 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = {
            code: 100
        }
    }
    res.send(json)
});
// 个人信息修改
router.post('/mesage', async (req, res) => {
    let data = req.body;
    let rows = await query('update userinfor SET userbornyear=' + data.userbornyear + ', userbornmonth=' + data.userbornmonth + ' ,userbornday=' + data.userbornday + ' where userphone='+ data.userphone);
    let json = {};
    if (rows.length != 0) {
        json = {
            code: 200,
            result: rows,

        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});

// 原手机号
router.post('/changeTelOne', async (req, res) => {
    let data = req.body;
    let Code, phoneHave, mimaHave;
    phoneHave = await query('select phone from userinfor where phone ='+data.phone);
    mimaHave = await query('select mima from userinfor where mima ='+data.mima);
    let rows = await query('select * from userinfor where phone ='+data.phone);
    if (phoneHave.length != 0) {
        if (mimaHave.length != 0) {
            Code = 1;
        } else {
            Code = 2;
        }
    } else {
        Code = 0;
    }
    let json = {
        code: Code,//0:账号错误 2:密码错误 1:成功
        phoneHave,
        result: rows,
    };
    res.send(json);
});
// 密码不变
router.post('/changeTelTwo', async (req, res) => {
    let data = req.body;
    let rows = await query('update userinfor SET phone=' + data.phone + ' where id='+ data.id);
    let json = {};
    if (rows.length != 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});

// 修改密码
router.post('/changepassword', async (req, res) => {
    let data = req.body;
    let rows = await query('update userinfor SET mima=' + data.newpassword + ' where phone='+ data.userphone);
    let json = {};
    if (rows.length != 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});
// 我的积分
router.post('/int', async (req, res) => {
    let data = req.body;
    let rows = await query("select * from userint");
    let json = {};
    if (!rows.length == 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = { code: 100 }
    }
    res.send(json)
});

// 地址管理
router.post('/address', async (req, res) => {
	let data = req.body;
	let rows = await query('select * from useraddress where state=\'1\'');
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});

// 删除地址
router.post('/addressdeleat', async (req, res) => {
	let data = req.body;
	let rows = await query('update useraddress SET state=\'2\' where id= '+ data.addressId);
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
// 设置默认
router.post('/addressmoren', async (req, res) => {
	let data = req.body;
	let rows = await query('update useraddress SET shemoren=\'1\' where id='+ data.addressId);
	// let rows = await query('update useraddress SET shemoren=\'0\' where id='+ data.addressId);
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
// 添加地址
router.post('/addressadd', async (req, res) => {
	let data = req.body;
	let rows = await query('insert into useraddress values(?,?,?,?,?,0,1,?);',
	[data.addName,data.addCity,data.addStree,data.addPhone,data.addBeizhu,data.userphone]);
	// let rows = await query('update useraddress SET shemoren=\'0\' where id='+ data.addressId);
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});


// 最近浏览
router.post('/pa', async (req, res) => {
	let data = req.body;
	let rows = await query("select * from addgoods where exist=\'1\'  ");
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
router.post('/del', async (req, res) => {
	let data = req.body;
      console.log(data.id);
	let rows =  await query('update addgoods set exist=\'0\' where id=' + data.id + ";");
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
// 我的收藏
router.post('/col', async (req, res) => {
	let data = req.body;
	let rows = await query("select * from shoucang where exist=\'1\'");
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
router.post('/change', async (req, res) => {
	let data = req.body;
console.log(data.id);
	let rows =  await query('update shoucang set exist=\'0\' where id=' + data.id + ";");
	let json = {};
	if (!rows.length == 0) {
		json = {
			code: 200,
			result: rows,
		}
	} else {
		json = {
			code: 100
		}
	}
	res.send(json)
});
// 我的消息
router.post('/news', async (req, res) => {
    let data = req.body;
    let rows = await query("select * from news where exist=\'1\'  ");
    let json = {};
    if (!rows.length == 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = {
            code: 100
        }
    }
    res.send(json)
});

// 删除消息
router.post('/delnews', async (req, res) => {
    let data = req.body;
    console.log(data.id);
    let rows = await query('update news set exist=\'0\' where id=' + data.id + ";");
    let json = {};
    if (!rows.length == 0) {
        json = {
            code: 200,
            result: rows,
        }
    } else {
        json = {
            code: 100
        }
    }
    res.send(json)
});

module.exports = router;
